package com.lyf.boot.modules.index.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lyf.boot.base.system.dao.BaseSmsConfigDao;
import com.lyf.boot.base.system.entity.SmsConfig;
import com.lyf.boot.constant.Constant;
import com.lyf.boot.service.ISmsConfigService;
import lombok.RequiredArgsConstructor;
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
import org.dromara.sms4j.cloopen.config.CloopenConfig;
import org.dromara.sms4j.comm.constant.SupplierConstant;
import org.dromara.sms4j.ctyun.config.CtyunConfig;
import org.dromara.sms4j.emay.config.EmayConfig;
import org.dromara.sms4j.huawei.config.HuaweiConfig;
import org.dromara.sms4j.jdcloud.config.JdCloudConfig;
import org.dromara.sms4j.lianlu.config.LianLuConfig;
import org.dromara.sms4j.provider.config.BaseConfig;
import org.dromara.sms4j.tencent.config.TencentConfig;
import org.dromara.sms4j.unisms.config.UniConfig;
import org.dromara.sms4j.yunpian.config.YunpianConfig;
import org.dromara.sms4j.zhutong.config.ZhutongConfig;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短信配置
 * @author Luyufan
 * @date 2023/12/8 11:03
 **/
@Service
@RequiredArgsConstructor
public class SmsConfigServiceImpl implements ISmsConfigService {

    private final BaseSmsConfigDao baseSmsConfigDao;


    @Override
    public BaseConfig getSupplierConfig(String configId) {
        SmsConfig smsConfig = baseSmsConfigDao.selectOne(Wrappers.<SmsConfig>lambdaQuery().eq(SmsConfig::getConfigId, configId));
        if(null == smsConfig){
            return null;
        }
        return convert(smsConfig);
    }

    @Override
    public List<BaseConfig> getSupplierConfigList() {
        List<SmsConfig> smsConfigList = baseSmsConfigDao.selectList(Wrappers.<SmsConfig>lambdaQuery()
                .eq(SmsConfig::getStatus, Constant.ZERO));
        if(CollUtil.isEmpty(smsConfigList)){
            return new ArrayList<>();
        }
        List<BaseConfig> baseConfigs = CollUtil.toList();
        smsConfigList.forEach(smsConfig -> {
            BaseConfig baseConfig = convert(smsConfig);
            if(null != baseConfig){
                baseConfigs.add(baseConfig);
            }
        });
        return baseConfigs;
    }

    private BaseConfig convert(SmsConfig smsConfig){
        if(null == smsConfig || StrUtil.isEmpty(smsConfig.getSupplier())){
            return null;
        }
        Map<String, BaseConfig> baseConfigMap = MapUtil.builder(new HashMap<String, BaseConfig>())
                .put(SupplierConstant.ALIBABA, new AlibabaConfig())
                .put(SupplierConstant.CLOOPEN, new CloopenConfig())
                .put(SupplierConstant.CTYUN, new CtyunConfig())
                .put(SupplierConstant.EMAY, new EmayConfig())
                .put(SupplierConstant.HUAWEI, new HuaweiConfig())
                .put(SupplierConstant.JDCLOUD, new JdCloudConfig())
                .put(SupplierConstant.NETEASE, new CtyunConfig())
                .put(SupplierConstant.TENCENT, new TencentConfig())
                .put(SupplierConstant.UNISMS, new UniConfig())
                .put(SupplierConstant.YUNPIAN, new YunpianConfig())
                .put(SupplierConstant.ZHUTONG, new ZhutongConfig())
                .put(SupplierConstant.LIANLU, new LianLuConfig())
                .build();
        BaseConfig baseConfig = baseConfigMap.get(smsConfig.getSupplier());
        if(null == baseConfig){
            return null;
        }
        BeanUtil.copyProperties(smsConfig, baseConfig);
        return baseConfig;
    }
}
